package cn.doitedu.demos

import cn.doitedu.dw.etl.DeviceIdAccountBind
import org.apache.spark.sql.SparkSession
import org.roaringbitmap.RoaringBitmap

import scala.collection.mutable

/**
 * @author 涛哥
 * @nick_name "deep as the sea"
 * @contact qq:657270652 wx:doit_edu
 * @site www.doitedu.cn
 * @date 2021-01-10
 * @desc doitedu trainning code
 */
object BitMapDemo {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName(DeviceIdAccountBind.getClass.getSimpleName)
      .master("local")
      .getOrCreate()
    import spark.implicits._

    spark.sql("show databases").show()


    val ds = spark.createDataset(Seq(1, 3, 5, 7, 2))
    val ids = ds.toDF("id").selectExpr("collect_set(id) as ids")
    ids.map(row=>{
      val idlst = row.getAs[mutable.WrappedArray[Int]]("ids")

      import scala.collection.JavaConversions._
      val bitmap = RoaringBitmap.bitmapOf()
      for (elem <- idlst) {
        bitmap.add(elem)
      }
      val arr: Array[Int] = bitmap.toArray
     arr
    }).map(arr=>{
      val bm = RoaringBitmap.bitmapOf(arr: _*)
      bm.contains(3)
    }).show()

    spark.close()

  }

}
